var app = getApp();
var md5 = require("../../utils/md5.js");
Page({
  data: {
    image:[
      {
        pic: '/images/0star.png',
        value: '1',
      },
      {
        pic: '/images/0star.png',
        value: '2',
      },
      {
        pic: '/images/0star.png',
        value: '3',
      },
      {
        pic: '/images/0star.png',
        value: '4',
      },
      {
        pic: '/images/0star.png',
        value: '5',
      },
    ],
    comment_number:0,
    img_add:[],
    is_niming:0,
    picture:[],
    orderId:'',
    pic_count: 0,
    shop_id:'',
    pic:''
  },
  onLoad: function (e) {
    var orderId = e.id;
    var shop_id = e.shop_id
    this.data.orderId = orderId;
    this.setData({
      orderId: orderId,
      userTel: app.globalData.userTel,
      shop_id:shop_id
    });
  },
  //处理星级
  star:function(e){
    var teht = this;
    var xing = e.currentTarget.dataset.index + 1;
    var xing_val = '';
    var image = '';
    var comment_number = teht.data.comment_number;
    if (xing == comment_number){
      xing_val = xing - 1;
      if (xing_val < 0){
        xing_val = 0;
      }
    }else{
      xing_val = xing;
    }
    console.log(xing_val)
    if (xing_val == 0) {
      image = [
        {
          pic: '/images/0star.png',
          value: '1',
        },
        {
          pic: '/images/0star.png',
          value: '2',
        },
        {
          pic: '/images/0star.png',
          value: '3',
        },
        {
          pic: '/images/0star.png',
          value: '4',
        },
        {
          pic: '/images/0star.png',
          value: '5',
        },
      ]
    } else if (xing_val == 1){
      image = [
        {
          pic: '/images/1star.png',
          value: '1',
        },
        {
          pic: '/images/0star.png',
          value: '2',
        },
        {
          pic: '/images/0star.png',
          value: '3',
        },
        {
          pic: '/images/0star.png',
          value: '4',
        },
        {
          pic: '/images/0star.png',
          value: '5',
        },
      ]
    } else if (xing_val == 2){
      image = [
        {
          pic: '/images/1star.png',
          value: '1',
        },
        {
          pic: '/images/1star.png',
          value: '2',
        },
        {
          pic: '/images/0star.png',
          value: '3',
        },
        {
          pic: '/images/0star.png',
          value: '4',
        },
        {
          pic: '/images/0star.png',
          value: '5',
        },
      ]
    } else if (xing_val == 3){
      image = [
        {
          pic: '/images/1star.png',
          value: '1',
        },
        {
          pic: '/images/1star.png',
          value: '2',
        },
        {
          pic: '/images/1star.png',
          value: '3',
        },
        {
          pic: '/images/0star.png',
          value: '4',
        },
        {
          pic: '/images/0star.png',
          value: '5',
        },
      ]
    } else if (xing_val == 4) {
      image = [
        {
          pic: '/images/1star.png',
          value: '1',
        },
        {
          pic: '/images/1star.png',
          value: '2',
        },
        {
          pic: '/images/1star.png',
          value: '3',
        },
        {
          pic: '/images/1star.png',
          value: '4',
        },
        {
          pic: '/images/0star.png',
          value: '5',
        },
      ]
    } else if (xing_val == 5) {
      image = [
        {
          pic: '/images/1star.png',
          value: '1',
        },
        {
          pic: '/images/1star.png',
          value: '2',
        },
        {
          pic: '/images/1star.png',
          value: '3',
        },
        {
          pic: '/images/1star.png',
          value: '4',
        },
        {
          pic: '/images/1star.png',
          value: '5',
        },
      ]
    }
    console.log(image)
    teht.setData({
      image: image,
      comment_number: xing_val,
    });
  },
  //图片上传/删除事件
  upload: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#4facb3",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImageShop('album')//从相册选择
          } else if (res.tapIndex == 1) {
            _this.chooseWxImageShop('camera')//调用拍照
          }
        }
      }
    })
  },
  /**
  * 调用图片接口
  */
  chooseWxImageShop: function (type) {
    let _this = this;
    var img_add = _this.data.img_add
    console.log(img_add);
    if (img_add.length < 8){
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        sourceType: [type],
        success: function (res) {
          img_add.push(
            {
              pic_img: res.tempFilePaths[0],
              type: 1
            }
          )
          console.log(img_add);
          _this.setData({
            img_add: img_add
          })
        }
      })
    }else{
      wx.showToast({
        title: '最多上传7张',
        icon: 'success',
        duration: 2000
      })
    }
  },
  /**
  * 删除数组中的图片
  */
  delUpload:function(e){
    let _this = this;
    var img_add = _this.data.img_add;
    var key = e.currentTarget.dataset.index;
    img_add.splice(key,1);
    console.log(img_add);
    _this.setData({
      img_add: img_add
    })
  },
  isNiming: function (e) {
     let _this = this;
    var value = e.detail.value;
    if (value == 1){
      _this.setData({
        is_niming: 1
      })
    }else{
      _this.setData({
        is_niming: 0
      })
    }
    console.log(_this.data.is_niming)
  },
  /**
  * 表单数据提交
  */
  formSubmit:function(e){
    var that = this;
    that.setData({
      pinglun_content: e.detail.value.pinglun_content
    })

    var service = 'User.AddPingLun';
    //var open_id = app.globalData.openId;
    //var user_id = app.globalData.userId;
    //var pinglun_vid = that.data.orderId;
    //var pinglun_content = e.detail.value.pinglun_content;
    //var xing_ji = that.data.comment_number;
    //var is_niming = that.data.is_niming;
    var picture_img = that.data.img_add;
    console.log(picture_img.length);
    if (picture_img.length == 0){

      that.savePinglun();
    }else{
      for (var i = 0; i < picture_img.length; i++) {

        that.getupImgs(picture_img[i].pic_img, picture_img.length)

        // if (i == picture_img.length - 1) {
        //     that.getupImgs(picture_img[i].pic_img, true)
        //   }else{
        //     that.getupImgs(picture_img[i].pic_img,false)
        //   }

        // if (picture_img[i].type == 1){
        //   if (i == picture_img.length-1){
        //     that.getupImgs(picture_img[i].pic_img, true)
        //   }else{
        //     that.getupImgs(picture_img[i].pic_img,false)
        //   }

        // }
      }
    }
    
   
  },
  
  //上传图片到服务器端
  getupImgs: function (e,status) {
    var that = this;
    var picture_list = [];
    picture_list=this.data.picture;
    
    var service = 'System.uploadImgWxa'
    var service_add = 'User.NewAddPingLun';
    var file = e
    var sign = md5.md5(service);
  
    wx.uploadFile({
      url: app.globalData.serviceUrl + service, //仅为示例，非真实的接口地址
      header: {
        'Content-Type': 'json'
      },
      filePath: e,
      name: 'file',
      formData: {
        'sign': sign,
      },
      success: function (res) { 
        var count = that.data.pic_count
        count++;

        var obj = JSON.parse(res.data)
         if (obj.data.code == 0) {
           var url = obj.data.info.file
           picture_list.push(url)
           that.setData({
             picture: picture_list,
             pic_count: count
           })

           if (count == status) {
             that.setData({
               pic_count: 0
             })
             var picimg = that.data.picture
             var pic = picimg + "";
             console.log(picimg + "");
             that.setData({
               pic: pic
             })
             that.savePinglun();
           }


         }
         

      },
      complete: function (res) {

      }
    })
    
  },
  savePinglun:function (){
    var that = this
    var is_niming = that.data.is_niming
    var open_id = app.globalData.openId
    var order_id = that.data.orderId
    var pinglun_content = that.data.pinglun_content
    var pinglun_vid = that.data.shop_id
    var service_new = 'User.NewAddPingLun';
    var user_id = app.globalData.userId
    var xingji = that.data.comment_number
    var pic = that.data.pic
    var sign = md5.md5(is_niming + open_id + order_id + pic + pinglun_content + pinglun_vid + service_new + user_id + xingji);
    wx.request({
      url: app.globalData.serviceUrl + service_new,
      data: {
        sign: sign,
        user_id: user_id,
        open_id: open_id,
        pinglun_vid: pinglun_vid,
        pinglun_content: pinglun_content,
        xing_ji: xingji,
        is_niming: is_niming,
        picture: pic,
        order_id: order_id
      },
      success: function (res) {
        console.log(res)
        if (res.data.data.code == 0) {
          wx.showToast({
            title: '评论成功',
            icon: 'success',
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/ucenter/order-list/index'
            })
          }, 1000)
        }
      },
    })
  }
})
 